package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.entity.AlarmDataEntity;
import com.ywjh.inter.entity.BloodGlucoseEntity;
import com.ywjh.inter.entity.BloodPressureEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.service.PatientDataService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Alarm extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4558965817855994335L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		int r = 0;
		String e = "";

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		JSONObject json = new JSONObject();
		switch (act) {
		case "get_by_patient_id":
			int user_id = data.getInt("user_id");
			int page = data.getInt("page");
			int num = data.getInt("num");
			List<AlarmDataEntity> patientAlramList = PatientDataService.getInstance().getPatientAlarmList(user_id, page,
					num);
			JSONArray array = new JSONArray();
			for (AlarmDataEntity ae : patientAlramList) {
				JSONObject jsonAe = new JSONObject();
				jsonAe.put("data_id", ae.getData_id());
				jsonAe.put("alarm_type", ae.getAlarm_type());
				jsonAe.put("time", StrUtil.getYYYYMMDDHHMISS(ae.getAdd_time()));
				jsonAe.put("data_type", ae.getData_type());
				ProfileEntity profileEntity = UsersService.getInstance().getProfileByUserId(ae.getUser_id());
				if (profileEntity != null) {
					jsonAe.put("patient_name", profileEntity.getName());
				} else {
					jsonAe.put("patient_name", "未设置");
				}
				jsonAe.put("patient_id", ae.getUser_id());
				UsersEntity users = UsersService.getInstance().findUserById(ae.getUser_id());
				if (users != null) {
					jsonAe.put("phone", users.getPhone());
					jsonAe.put("hx_username", "user_" + users.getId());
					jsonAe.put("head_url", SystemConfig.SERVER_DOMAIN + users.getHead_url());
				}

				switch (ae.getAlarm_type()) {
				case "bglu":
					System.out.println("bglu");
					BloodGlucoseEntity entity = PatientDataService.getInstance().findBloodGlucoseById(ae.getData_id());
					if (entity != null) {
						JSONObject bglu = new JSONObject();
						bglu.put("data", entity.getData());
						bglu.put("memo", entity.getMemo());
						bglu.put("check_time", StrUtil.getYYYYMMDDHHMISS(entity.getCheck_time()));
						bglu.put("plan_id", entity.getPlan_id());
						jsonAe.put("data", bglu);
					}
					break;
				case "bpre":
					System.out.println("bpre");
					BloodPressureEntity entity2 = PatientDataService.getInstance()
							.findBloodPressureById(ae.getData_id());
					if (entity2 != null) {
						JSONObject bpre = new JSONObject();
						bpre.put("hi", entity2.getHi());
						bpre.put("low", entity2.getLow());
						bpre.put("check_time", StrUtil.getYYYYMMDDHHMISS(entity2.getCheck_time()));
						bpre.put("pulse", entity2.getPulse());
						jsonAe.put("data", bpre);
					}
					break;
				}
				array.add(jsonAe);
			}
			r = 1;
			json.put("d", array);
			break;
		case "get_by_doc_id":
			int doc_id = data.getInt("doc_id");
			page = data.getInt("page");
			num = data.getInt("num");
			List<AlarmDataEntity> docAlramList = PatientDataService.getInstance().getPatientAlarmListByDocId(doc_id,
					page, num);
			array = new JSONArray();
			for (AlarmDataEntity ae : docAlramList) {
				JSONObject jsonAe = new JSONObject();
				jsonAe.put("data_id", ae.getData_id());
				jsonAe.put("alarm_type", ae.getAlarm_type());
				jsonAe.put("time", StrUtil.getYYYYMMDDHHMISS(ae.getAdd_time()));
				jsonAe.put("data_type", ae.getData_type());
				ProfileEntity profileEntity = UsersService.getInstance().getProfileByUserId(ae.getUser_id());
				if (profileEntity != null) {
					jsonAe.put("patient_name", profileEntity.getName());
				} else {
					jsonAe.put("patient_name", "未设置");
				}
				UsersEntity users = UsersService.getInstance().findUserById(ae.getUser_id());
				if (users != null) {
					jsonAe.put("phone", users.getPhone());
					jsonAe.put("hx_username", "user_" + users.getId());
					jsonAe.put("head_url", SystemConfig.SERVER_DOMAIN + users.getHead_url());
				}
				jsonAe.put("patient_id", ae.getUser_id());
				switch (ae.getAlarm_type()) {
				case "bglu":
					BloodGlucoseEntity entity = PatientDataService.getInstance().findBloodGlucoseById(ae.getData_id());
					if (entity != null) {
						JSONObject bglu = new JSONObject();
						bglu.put("data", entity.getData());
						bglu.put("memo", entity.getMemo());
						bglu.put("check_time", StrUtil.getYYYYMMDDHHMISS(entity.getCheck_time()));
						bglu.put("plan_id", entity.getPlan_id());
						jsonAe.put("data", bglu);
					}
					break;
				case "bpre":
					BloodPressureEntity entity2 = PatientDataService.getInstance()
							.findBloodPressureById(ae.getData_id());
					if (entity2 != null) {
						JSONObject bpre = new JSONObject();
						bpre.put("hi", entity2.getHi());
						bpre.put("low", entity2.getLow());
						bpre.put("memo", entity2.getMemo());
						bpre.put("check_time", StrUtil.getYYYYMMDDHHMISS(entity2.getCheck_time()));
						bpre.put("pulse", entity2.getPulse());
						jsonAe.put("data", bpre);
					}
					break;
				}
				array.add(jsonAe);
			}
			r = 1;
			json.put("d", array);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
